/**
 * Created by FDD on 2017/5/24.
 * @desc 双线
 * @Inherits ol.geom.Polygon
 */
import { Map } from 'ol'
import { Polygon } from 'ol/geom'
import { TWOLINES } from '../../Utils/PlotTypes'
import * as PlotUtils from '../../Utils/utils'
import * as Constants from '../../Constants'
class TwoLines extends Polygon {
    constructor(coordinates, points, params) {
        super([])
        this.type = TWOLINES
        /**宽度因子 */
        this.widthFactor = 0.05
        this.freehand = false

        this.set('params', params)
        if (points && points.length > 0) {
            this.setPoints(points)
        } else if (coordinates && coordinates.length > 0) {
            this.setCoordinates(coordinates)
        }
    }

    /**
     * 获取标绘类型
     * @returns {*}
     */
    getPlotType() {
        return this.type
    }

    /**
     * 执行动作
     */
    generate() {
        try {
            let cont = this.getPointCount()
            if (cont < 2) {
                return false
            } else {
                let pnts = this.getPoints()
                let len = PlotUtils.getBaseLength(pnts)
                let width = len * this.widthFactor

                let pList = []
                let pList2 = []
                for (let i = 0; i < pnts.length - 1; i++) {
                    let [pnt1, pnt2] = [pnts[i], pnts[i + 1]]
                    /** 头左 */
                    let tailLeft = PlotUtils.getThirdPoint(pnt2, pnt1, Constants.HALF_PI, width, true)
                    /** 头右 */
                    let tailRight = PlotUtils.getThirdPoint(pnt2, pnt1, Constants.HALF_PI, width, false)
                    /** 尾左 */
                    let neckLeft = PlotUtils.getThirdPoint(pnt1, pnt2, Constants.HALF_PI, width, false)
                    /** 尾右 */
                    let neckRight = PlotUtils.getThirdPoint(pnt1, pnt2, Constants.HALF_PI, width, true)

                    pList.push(tailLeft)
                    pList.push(neckLeft)
                    pList2.push(tailRight)
                    pList2.push(neckRight)
                }
                // 克隆后颠倒数组，然后重新添加，形成闭环。因为是多边形，单线多个点会形成闭环
                let top = pList.clone().reverse()
                let down = pList2.clone().reverse()
                pList.forEach(it => top.push(it));
                pList2.forEach(it => down.push(it));

                this.setCoordinates([top, down])
            }
        } catch (e) {
            console.log(e)
        }
    }

    /**
     * 设置地图对象
     * @param map
     */
    setMap(map) {
        if (map && map instanceof Map) {
            this.map = map
        } else {
            throw new Error('传入的不是地图对象！')
        }
    }

    /**
     * 获取当前地图对象
     * @returns {Map|*}
     */
    getMap() {
        return this.map
    }

    /**
     * 判断是否是Plot
     * @returns {boolean}
     */
    isPlot() {
        return true
    }

    /**
     * 设置坐标点
     * @param value
     */
    setPoints(value) {
        this.points = !value ? [] : value
        if (this.points.length >= 1) {
            this.generate()
        }
    }

    /**
     * 获取坐标点
     * @returns {Array.<T>}
     */
    getPoints() {
        return this.points.slice(0)
    }

    /**
     * 获取点数量
     * @returns {Number}
     */
    getPointCount() {
        return this.points.length
    }

    /**
     * 更新当前坐标
     * @param point
     * @param index
     */
    updatePoint(point, index) {
        if (index >= 0 && index < this.points.length) {
            this.points[index] = point
            this.generate()
        }
    }

    /**
     * 更新最后一个坐标
     * @param point
     */
    updateLastPoint(point) {
        this.updatePoint(point, this.points.length - 1)
    }

    /**
     * 结束绘制
     */
    finishDrawing() {
    }
}

export default TwoLines
